import random
import itertools
from dictionary import SimpleDictionary as Dictionary
dictionary = Dictionary.load()
import sys

dice = [
'LYOBLOW',
'AEAAEE',
'COOPMW',
'IRHTBK',
'EEEAAA',
'RUSHFI',
'TLNRDO',
'ZXQJVE',
'GUSMRI',
'FUITNP',
]

red_dice = [
'BPSYQF',
'MKCGJD',
'SNLWVH',
]

MIN_LENGTH = 3
MAX_LENGTH = 10
MAX_WORDS = 5

def roll():
	letters = []
	for die in dice + red_dice:
		letter = random.choice(die)
		letters.append(letter)
	
	return letters

"""def main():
	
	results = []
	
	summations = [0]*(MAX_LENGTH+1)
	averages = [0]*(MAX_LENGTH+1)
	
	for i in xrange(10):
		results.append([0]*(MAX_LENGTH+1))
		soln = solve(roll())
		for word in soln:
			results[i][len(word)] += 1
		for res in results[i]:
			summations[i] += res
		
	for i,summ in enumerate(summations):
		averages[i] += summ/len(results)
		

	print results
	print summations
	print averages"""
	
def main(args):
	
	if len(args)>1:
		print "Using dice: %s..." % list(args[1])
		res = solve(args[1])
	else:
		print "Rolling dice... ",
		dice = roll()
		print "Using dice: %s..." % dice
		res = solve(dice)
	print "Every possible word (sorted by length and then lexographically)"
	res.sort()
	res.sort(lambda x,y: cmp(len(x),len(y)))
	print "Found %s words" % len(res)
	print res

def solve(letters):
	soln = escape_on_bad_prefix(letters,dictionary)
	#soln.sort(lambda a,b: cmp(len(a),len(b) if len(a) != len(b) else cmp(a,b)))
	return soln

def escape_on_bad_prefix(letters,dictionary):
	
	letters = [x.lower() for x in letters]

	def check(checking,remaining_letters):
		if not dictionary.checkPrefix(checking):
			return
		if len(checking) > MAX_LENGTH:
			return
			
		for l in remaining_letters:
			leftovers = tuple([j for j in remaining_letters if j != l])
			check(checking + l, leftovers)
		
		if dictionary.checkWord(checking) \
			and len(checking) >= MIN_LENGTH \
			and checking not in found_words:
				#print found_words
				found_words.append(checking)
		
	found_words = []
		
	check('',letters)
	
	return found_words

def brute_force(letters,dictionary):
	found_words = []
	
	for word_length in xrange(MIN_LENGTH,MAX_LENGTH):
		words_this_size = []
		for check_word in itertools.permutations(letters,word_length):
			check_word = ''.join(check_word).lower()
			if check_word in found_words:
				continue
			if dictionary.checkWord(check_word):
				print check_word
				found_words.append(check_word)
				words_this_size.append(check_word)
				if len(words_this_size) == MAX_WORDS:
					break

	return found_words

def randomly_check_a_million_times(letters,dictionary):
	
	found_words = []
	
	for word_length in xrange(MIN_LENGTH, MAX_LENGTH):
		words_this_size = []
		count = 0
		while count < 1000000:
			count += 1
			check_word = ''.join(random.sample(letters,word_length)).lower()
			if dictionary.checkWord(check_word):
				print check_word;
				found_words.append(check_word)
				words_this_size.append(check_word)
				if len(words_this_size) == MAX_WORDS:
					break
	
	return found_words

if __name__ == '__main__':	
	main(sys.argv)